/*
* Copyright (c) 2008, Vladimir Gumenuk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the copyright holder nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

// ChildFrm.cpp : implementation of the CChildFrame class
//
#include "stdafx.h"
#include "RealGNX4Lib.h"

#include "ChildFrm.h"
#include "LeftView.h"
#include "RealGNX4LibView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CChildFrame

IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd)

BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd)
END_MESSAGE_MAP()


// CChildFrame construction/destruction

CChildFrame::CChildFrame()
{
	// TODO: add member initialization code here
}

CChildFrame::~CChildFrame()
{
}

BOOL CChildFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/, CCreateContext* pContext)
{
	// create splitter window
	if( !m_wndSplitter.CreateStatic(this, 1, 2) )
		return FALSE;

	if( !m_wndSplitter.CreateView( 0, 0, RUNTIME_CLASS(CLeftView), CSize(160, 100), pContext ) ||
		!m_wndSplitter.CreateView( 0, 1, RUNTIME_CLASS(CRealGNX4LibView), CSize(100, 100), pContext ) )
	{
		m_wndSplitter.DestroyWindow();
		return FALSE;
	}

    CLeftView *pLeft = (CLeftView *)m_wndSplitter.GetPane( 0, 0 );
    ASSERT( pLeft->IsKindOf( RUNTIME_CLASS(CLeftView) ) );
    pLeft->SetRightView( GetRightPane() );

	return TRUE;
}

BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying the CREATESTRUCT cs
	if( !CMDIChildWnd::PreCreateWindow(cs) )
		return FALSE;

	return TRUE;
}


// CChildFrame diagnostics

#ifdef _DEBUG
void CChildFrame::AssertValid() const
{
	CMDIChildWnd::AssertValid();
}

void CChildFrame::Dump(CDumpContext& dc) const
{
	CMDIChildWnd::Dump(dc);
}

#endif //_DEBUG

CRealGNX4LibView* CChildFrame::GetRightPane()
{
    return (CRealGNX4LibView*)m_wndSplitter.GetPane( 0, 1 );
}

CLeftView * CChildFrame::GetLeftPane()
{
    return (CLeftView *)m_wndSplitter.GetPane( 0, 0 );
}
// CChildFrame message handlers


BOOL CChildFrame::CanMoveUp()
{
    CLeftView *pLeft = GetLeftPane();
    int iNo = pLeft->GetSelectedPreset();
    return ( iNo > 0 ) ? TRUE : FALSE;
}

BOOL CChildFrame::CanMoveDown()
{
    CLeftView *pLeft = GetLeftPane();
    int iNo = pLeft->GetSelectedPreset();
    return ( iNo < (GNX_MAX_PRESETS-1) ) ? TRUE : FALSE;
}

void CChildFrame::MovePresetUp()
{
    CLeftView        *pLeft  = GetLeftPane();
    CRealGNX4LibView *pRight = GetRightPane();
    CRealGNX4LibDoc  *pDoc   = pRight->GetDocument();

    UINT iSel = pLeft->GetSelectedPreset();
    if( pDoc->MovePresetUp( iSel ) )
        pLeft->MovePresetUp();
}

void CChildFrame::MovePresetDown()
{
    CLeftView        *pLeft  = GetLeftPane();
    CRealGNX4LibView *pRight = GetRightPane();
    CRealGNX4LibDoc  *pDoc   = pRight->GetDocument();

    UINT iSel = pLeft->GetSelectedPreset();
    if( pDoc->MovePresetDown( iSel ) )
        pLeft->MovePresetDown();
}

void CChildFrame::LoadG4PFile( LPCSTR lpszFileName )
{
    CLeftView        *pLeft  = GetLeftPane();
    CRealGNX4LibView *pRight = GetRightPane();
    CRealGNX4LibDoc  *pDoc   = pRight->GetDocument();

    UINT iSel = pLeft->GetSelectedPreset();

    if( pDoc->LoadGP4FileToPreset( lpszFileName, iSel ) )
    {
        pLeft->UpdatePreset( iSel );
    }
}
